package com.ajk.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;

import com.ajk.server.model.Doctor;
import com.ajk.server.model.query.DoctorQuery;
import com.ajk.server.utils.IntegerUtil;
/**
 * 医生  <br>
 * t_doctor <br>
 * v1.0 2016-05-07
 * @see com.ajk.server.dao.DoctorMapper
 */
 public class DoctorProvider {
 
 	/**
 	* 新增 医生
 	*/
 	public String insert(Doctor domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into t_doctor(create_time,last_modify_time");
	 	if(domain.getUserId()!=null){
			sb.append(",user_id");
		}
	 	if(domain.getTitleId()!=null){
			sb.append(",title_id");
		}
	 	if(domain.getHospitalId()!=null){
			sb.append(",hospital_id");
		}
	 	if(domain.getDepartmentId()!=null){
			sb.append(",department_id");
		}
	 	if(domain.getIntroduction()!=null){
			sb.append(",introduction");
		}
	 	if(domain.getResume()!=null){
			sb.append(",resume");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",in_used");
		}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getUserId()!=null){
			sb.append(",#{userId}");
		}
	 	if(domain.getTitleId()!=null){
			sb.append(",#{titleId}");
		}
	 	if(domain.getHospitalId()!=null){
			sb.append(",#{hospitalId}");
		}
	 	if(domain.getDepartmentId()!=null){
			sb.append(",#{departmentId}");
		}
	 	if(domain.getIntroduction()!=null){
			sb.append(",#{introduction}");
		}
	 	if(domain.getResume()!=null){
			sb.append(",#{resume}");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",#{inUsed}");
		}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 医生
 	*/
	public String update(Doctor domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update t_doctor set last_modify_time = now() ");
		
		if(domain.getUserId()!=null){
			sb.append(",user_id=#{userId}");
		}
		if(domain.getTitleId()!=null){
			sb.append(",title_id=#{titleId}");
		}
		if(domain.getHospitalId()!=null){
			sb.append(",hospital_id=#{hospitalId}");
		}
		if(domain.getDepartmentId()!=null){
			sb.append(",department_id=#{departmentId}");
		}
		if(domain.getIntroduction()!=null){
			sb.append(",introduction=#{introduction}");
		}
		if(domain.getResume()!=null){
			sb.append(",resume=#{resume}");
		}
		if(domain.getInUsed()!=null){
			sb.append(",in_used=#{inUsed}");
		}
		
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<Doctor>> map){
		StringBuffer sb = new StringBuffer();
		List<Doctor> doctors = map.get("list");
		sb.append("insert into t_doctor(create_time,last_modify_time,user_id,title_id,hospital_id,department_id,introduction,resume,in_used) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].userId}"
	    		+ ",#'{'list[{0}].titleId}"
	    		+ ",#'{'list[{0}].hospitalId}"
	    		+ ",#'{'list[{0}].departmentId}"
	    		+ ",#'{'list[{0}].introduction}"
	    		+ ",#'{'list[{0}].resume}"
	    		+ ",#'{'list[{0}].inUsed}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(doctors)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  doctors.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 医生
 	*/
	public String findByQuery(DoctorQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from t_doctor where 1>0 ");
		
		if(IntegerUtil.isPositiveInteger(query.getInUsed())){
			sb.append(" and in_used=#{inUsed}");
		}
		
		if (CollectionUtils.isNotEmpty(query.getHospitalIds())) {
			sb.append(" and hospital_id in (#{hospitalIds[0]}");
			for (int i = 1; i < query.getHospitalIds().size(); i++) {
				sb.append(",#{hospitalIds[").append(i).append("]}");
			}
			sb.append(")");
		}
		
		if(IntegerUtil.isPositiveInteger(query.getDepartmentId())){
			sb.append(" and department_id=#{departmentId}");
		}
		
		if(IntegerUtil.isPositiveInteger(query.getStartId())){
			sb.append(" and user_id>#{startId} order by user_id limit #{limit}");
		}else if(IntegerUtil.isPositiveInteger(query.getEndId())){
			sb.append(" and user_id<#{endId} order by user_id desc limit #{limit}");
		}else if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by user_id desc limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by user_id desc limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(DoctorQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from t_doctor where 1>0 ");
		
		if(IntegerUtil.isPositiveInteger(query.getInUsed())){
			sb.append(" and in_used=#{inUsed}");
		}
		
		if (CollectionUtils.isNotEmpty(query.getHospitalIds())) {
			sb.append(" and hospital_id in (#{hospitalIds[0]}");
			for (int i = 1; i < query.getHospitalIds().size(); i++) {
				sb.append(",#{hospitalIds[").append(i).append("]}");
			}
			sb.append(")");
		}
		
		if(IntegerUtil.isPositiveInteger(query.getDepartmentId())){
			sb.append(" and department_id=#{departmentId}");
		}
		
		
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 医生
 	*/
 	public String findByUserIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from t_doctor where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" user_id=").append("#{list[0]}");
			}else{
				sb.append(" user_id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 }	